package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper

import android.text.Html
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.IItemTouchHelperAdapter
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.OnStartDragListener
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletPaperListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_template.view.*
import java.util.*

class AibkGenerateTempletAdapter(
        val activity: AppCompatActivity,
        var templetPaperListInfor: TempletPaperListInfor,
        val dragListener: OnStartDragListener
) : KBaseRecyclerAdapter(), IItemTouchHelperAdapter {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_template, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        val templetName = templetPaperListInfor[holder.adapterPosition].TempletName
        val typeCount = templetPaperListInfor[holder.adapterPosition].TypeCount
        val itemCount = templetPaperListInfor[holder.adapterPosition].ItemCount
        val fullScore = templetPaperListInfor[holder.adapterPosition].FullScore
        val isChecked = templetPaperListInfor[holder.adapterPosition].IsChecked
        val resource = templetPaperListInfor[position].Resource

        if (resource == 0) {
            holder.convertView.IvDeleteFormPlate.setImageResource(R.mipmap.aibk_icon_delete_templet_off)
        } else {
            holder.convertView.IvDeleteFormPlate.setImageResource(R.mipmap.aibk_icon_delete_templet)
        }

        //显示模板名称
        holder.convertView.TvTempletName.text = templetName
        //显示模板概要（x大题，y小题，z分值）
        holder.convertView.TvTempletInfor.text =
                Html.fromHtml(activity.resources.getString(R.string.aibk_templet_infor, typeCount, itemCount, LancooMethod.killlingDouble(fullScore)))
        //显示模板选中状态
        holder.convertView.BtnCheck.isSelected = isChecked
        //模板勾选
        holder.convertView.BtnCheck.setOnClickListener {
            holder.convertView.BtnCheck.isSelected = !holder.convertView.BtnCheck.isSelected
            for (i in 0 until templetPaperListInfor.size) {
                if (i == holder.adapterPosition) {
                    templetPaperListInfor[i].IsChecked = holder.convertView.BtnCheck.isSelected
                } else {
                    templetPaperListInfor[i].IsChecked = false
                }
            }
            onSwipeListener!!.onCheck()
            notifyDataSetChanged()
        }
        //模板拖动交换
        holder.convertView.LlItemView.setOnLongClickListener(View.OnLongClickListener {
//            dragListener.onStartDrag(holder)//此处屏蔽，即为屏蔽拖动排序事件
            false
        })


        //模板点击事件
        holder.convertView.LlItemView.setOnClickListener(View.OnClickListener {
            Log.e("==显示==", "==index==" + holder.adapterPosition + "===========点击的内容==============" + templetPaperListInfor[holder.adapterPosition].TempletName)
            onSwipeListener!!.onPreview(holder.itemView, holder.adapterPosition)//position 为原来初始化的序号
        })
        //模板删除
        holder.convertView.IvDeleteFormPlate.setOnClickListener(View.OnClickListener {
            if (null != onSwipeListener) {
                //且如果想让侧滑菜单同时关闭，需要同时调用
                if(resource == 0){
                    MyToast.showToast("系统模板不支持删除")
                }else {
                    onSwipeListener!!.onDelete(holder.itemView, holder.adapterPosition, templetPaperListInfor.size)
                    templetPaperListInfor.removeAt(position)
                    notifyItemRemoved(holder.adapterPosition)
                    Log.e("==显示==", "===========最新删除的position===============" + templetPaperListInfor.size)
                }
            }
        })
    }

    override fun getItemCount(): Int {
        return templetPaperListInfor.size
    }

    override fun onItemDismiss(position: Int) {
        //不做操作，但不能删除
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int) {
        Collections.swap(templetPaperListInfor, fromPosition, toPosition)
        notifyItemMoved(fromPosition, toPosition)
    }

    interface OnSwipeListener {
        fun onPreview(view: View?, pos: Int)
        fun onDelete(view: View?, pos: Int, totalCount: Int)
        fun onCheck()
    }

    private var onSwipeListener: OnSwipeListener? = null

    fun setOnDelListener(mOnDelListener: OnSwipeListener?) {
        onSwipeListener = mOnDelListener
    }

    fun refreshView() {
        notifyDataSetChanged()
    }

    fun getTempletListInforAndRefresh(templetPaperListInforX: TempletPaperListInfor){
        templetPaperListInfor = templetPaperListInforX
        notifyDataSetChanged()
    }

}